package com.ruoyi.custom.service.impl;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;

import com.ruoyi.custom.domain.Member;
import com.ruoyi.custom.mapper.MemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.custom.mapper.RechargeRecordMapper;
import com.ruoyi.custom.domain.RechargeRecord;
import com.ruoyi.custom.service.IRechargeRecordService;
import com.ruoyi.common.core.text.Convert;

/**
 * 充值管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-08
 */
@Service
public class RechargeRecordServiceImpl implements IRechargeRecordService {
	@Autowired
	private RechargeRecordMapper rechargeRecordMapper;

	@Autowired
	private MemberMapper memberMapper;

	/**
	 * 查询充值管理
	 *
	 * @param id 充值管理主键
	 * @return 充值管理
	 */
	@Override
	public RechargeRecord selectRechargeRecordById(Long id) {
		return rechargeRecordMapper.selectRechargeRecordById(id);
	}

	/**
	 * 查询充值管理列表
	 *
	 * @param rechargeRecord 充值管理
	 * @return 充值管理
	 */
	@Override
	public List<RechargeRecord> selectRechargeRecordList(RechargeRecord rechargeRecord) {
//		System.out.println("搜索参数--->"+rechargeRecord);
		return rechargeRecordMapper.selectRechargeRecordList(rechargeRecord);
	}

	/**
	 * 新增充值管理
	 *
	 * @param rechargeRecord 充值管理
	 * @return 结果
	 */
	@Override
	public int insertRechargeRecord(RechargeRecord rechargeRecord) {

		//获取会员信息
		Member member = memberMapper.selectMemberById(rechargeRecord.getMemberId());
		//获取余额
		BigDecimal balance = member.getBalance();

		//获取累计充值金额
		String keyOne = member.getKeyOne();

		//获取该笔充值金额
		BigDecimal rechargeAmount = rechargeRecord.getRechargeAmount();

		BigDecimal newRechargeAmount;

		// 判断 rechargeAmount 的值，并重新赋值
		switch (rechargeAmount.intValue()) {
//			会员充卡1000元10次，2000元22次
			case 1000:
				newRechargeAmount = new BigDecimal("10");
				break;
			case 2000:
				newRechargeAmount = new BigDecimal("22");
				break;
			default:
				// 如果没有匹配的条件，可以选择保持原值或执行其他操作
				newRechargeAmount = new BigDecimal("0");
				break;
		}

		//更改会员余额
		member.setBalance(balance.add(newRechargeAmount));

		//设置累计金额
		member.setKeyOne(new BigDecimal(keyOne).add(rechargeAmount).toString());
		memberMapper.updateMember(member);

		//添加时间
		rechargeRecord.setRechargeTime(new Timestamp(System.currentTimeMillis()));

		//isVisible显示为true
		rechargeRecord.setIsVisible(1);

		return rechargeRecordMapper.insertRechargeRecord(rechargeRecord);
	}



	/**
	 * 修改充值管理
	 *
	 * @param rechargeRecord 充值管理
	 * @return 结果
	 */
	@Override
	public int updateRechargeRecord(RechargeRecord rechargeRecord) {
		return rechargeRecordMapper.updateRechargeRecord(rechargeRecord);
	}

	/**
	 * 批量删除充值管理
	 *
	 * @param ids 需要删除的充值管理主键
	 * @return 结果
	 */
	@Override
	public int deleteRechargeRecordByIds(String ids) {
		return rechargeRecordMapper.deleteRechargeRecordByIds(Convert.toStrArray(ids));
	}

	/**
	 * 删除充值管理信息
	 *
	 * @param id 充值管理主键
	 * @return 结果
	 */
	@Override
	public int deleteRechargeRecordById(Long id) {
		return rechargeRecordMapper.deleteRechargeRecordById(id);
	}
}
